package com.lpf.jsonp;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TestJsonpDemo extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public TestJsonpDemo() {
		super();
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 回调函数名
		String callBack = request.getParameter("callback");
		// json数据
		String jsonData = "{\"name\":\"挖坑埋你\"}";
		// jsonp格式的数据
		String jsonp = callBack + "(" + jsonData + ")";
		// 设置头信息
		response.setHeader("Content-type", "application/json;charset=utf-8");
		// 输出jsonp格式的数据
		response.getWriter().println(jsonp);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
